package it.fuesi.expired.model.base;

import java.util.ArrayList;
import java.util.List;

public class ModelCollection<T extends IModel> extends AModel {

	private List<T> list;
	private int count = 0;

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}
	
	public void add(T model){
		if(list == null)
			list = new ArrayList<T>();
		
		list.add(model);
	}
	
	public void setCount(int count){
		this.count = count;
	}
	
	public int getCount(){
		return list == null ? 0 : list.size();
	}
	
	public T get(int position){
		if(list != null && list.size() > position)
			return list.get(position);
		
		return null;
	}
	
	public T remove(int position){
		return list.remove(position);
	}
	
	public T remove(String id){
		int pos = 0;
		for(T item : list){
			if(item.get_id().equals(id))
				 break;
			pos++;
		}
		return remove(pos);
	}
	
	public static <T extends ModelCollection<IModel>> T factory(Class<T> modelCollectionClass) throws InstantiationException, IllegalAccessException{
		return modelCollectionClass.newInstance();
	}
	
	@Override
	public String toString() {
		final StringBuffer sb = new StringBuffer(super.toString());
		sb.append(" - count: " + count);
		sb.append(" - list: " + list);
		return sb.toString();
	}
	
	
}
